package com.oa.official.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oa.official.entity.process.Process;
import com.oa.official.entity.process.ProcessTemplate;
import com.oa.official.entity.process.ProcessType;
import com.oa.official.response.Result;
import com.oa.official.service.OaProcessService;
import com.oa.official.service.OaProcessTemplateService;
import com.oa.official.service.OaProcessTypeService;
import com.oa.official.service.SysUserService;
import com.oa.official.vo.process.ApprovalVo;
import com.oa.official.vo.process.ProcessFormVo;
import com.oa.official.vo.process.ProcessVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Api(tags = "审批流管理")
@RestController
@RequestMapping(value="/admin/process")
@CrossOrigin
public class AppProcessController {

    @Resource
    private OaProcessTypeService oaProcessTypeService;

    @Resource
    private OaProcessTemplateService oaProcessTemplateService;

    @Resource
    private OaProcessService oaProcessService;

    @Resource
    private SysUserService sysUserService;

    //查询所有审批分类和每个分类所有审批模板
    @ApiOperation("查询所有审批分类和每个分类所有审批模板")
    @GetMapping("/findProcessType")
    public Result findProcessType() {
        List<ProcessType> list = oaProcessTypeService.findProcessType();
        return Result.success(list);
    }

    //获取审批模板数据
    @ApiOperation("根据模板id获取模板详情信息")
    @GetMapping("/getProcessTemplate/{processTemplateId}")
    public Result getProcessTemplate(@PathVariable Long processTemplateId) {
        ProcessTemplate processTemplate = oaProcessTemplateService.getById(processTemplateId);
        return Result.success(processTemplate);
    }


    @ApiOperation(value = "提交申请-启动流程")
    @PostMapping("/startUp")
    public Result startUp(@RequestBody ProcessFormVo processFormVo) {
        oaProcessService.startUp(processFormVo);
        return Result.success();
    }


    @ApiOperation(value = "代办任务列表")
    @GetMapping("/findPending/{page}/{limit}")
    public Result findPending(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {
        Page<Process> pageParam = new Page<>(page,limit);
        IPage<ProcessVo> pageModel = oaProcessService.findfindPending(pageParam);
        return Result.success(pageModel);
    }


    //查看审批详情信息
    @ApiOperation(value = "查看审批详情信息")
    @GetMapping("/show/{id}")
    public Result show(@PathVariable Long id) {
        Map<String,Object> map = oaProcessService.show(id);
        return Result.success(map);
    }

    //审批
    @ApiOperation(value = "审批")
    @PostMapping("/approve")
    public Result approve(@RequestBody ApprovalVo approvalVo) {
        oaProcessService.approve(approvalVo);
        return Result.success();
    }

    @ApiOperation(value = "历史已办任务")
    @GetMapping("/findProcessed/{page}/{limit}")
    public Result findProcessed(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {
        Page<Process> pageParam = new Page<>(page,limit);
        IPage<ProcessVo> pageModel = oaProcessService.findProcessed(pageParam);
        return Result.success(pageModel);
    }

    @ApiOperation(value = "我提交的任务")
    @GetMapping("/findStarted/{page}/{limit}")
    public Result findStarted(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {
        Page<ProcessVo> pageParam = new Page<>(page, limit);
        IPage<ProcessVo> pageModel = oaProcessService.findStarted(pageParam);
        return Result.success(pageModel);
    }
    @ApiOperation(value = "获取当前登录用户的信息")
    @GetMapping("/getCurrentUser")
    public Result getCurrentUser() {
        return sysUserService.getCurrentUser();
    }
}